﻿using System;
using System.Data;
using System.IO;
using System.IO.Compression;
namespace Common
{
    /// <summary>
    /// Summary description for Cop
    /// </summary>
    public class Cop
    {
        public Cop()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="fileName">文件名(全路径)</param>
        /// <param name="data">需要压缩的字符串</param>
        public void CompressFile(string fileName, string data)
        {
            FileStream fstream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
            GZipStream gstream = new GZipStream(fstream, CompressionMode.Compress);
            StreamWriter swriter = new StreamWriter(gstream);
            swriter.Write(data);
            swriter.Close();
            gstream.Close();
            fstream.Close();
        }
        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="fileName">文件名(全路径)</param>
        /// <returns></returns>
        public string DecompressFile(string fileName)
        {
            string cstring = "";
            FileStream fstream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            GZipStream gstream = new GZipStream(fstream, CompressionMode.Decompress);
            StreamReader reader = new StreamReader(gstream);
            cstring = reader.ReadToEnd();
            reader.Close();
            gstream.Close();
            fstream.Close();
            return cstring;
        }
    }

}

